package com.wondersgroup.fjzlpt.console.service.impl.menu;

import java.util.Map;

import com.wondersgroup.fjzlpt.console.feign.FormFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.console.dao.inter.menu.MenuActionDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.menu.MenuDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuActionDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.role.RoleMenuDAO;
import com.wondersgroup.fjzlpt.console.service.inter.menu.MenuService;
import org.springframework.util.StringUtils;


@Service
public class MenuServiceImpl implements MenuService{

	/**菜单*/
	@Autowired
	private MenuDAO menuDao;
	/**角色菜单*/
	@Autowired
	private RoleMenuDAO roleMenuDAO;
	/**角色菜单功能*/
	@Autowired
	private RoleMenuActionDAO roleMenuActionDAO;
	/**菜单功能*/
	@Autowired
	private MenuActionDAO menuActionDAO;


	@Autowired
	private FormFeignService formFeignService;
	/**  运行评价在mongodb中的表名 */
	private static final String TABLE_NAME = "yxpjpz";
	
	
	@Override
	@Cacheable("queryMenu")
	public JSONArray queryMenu(){
		return menuDao.queryMenuAndActions();
	}
	
	@Override
	@CacheEvict(value={"queryMenu"}, allEntries=true)  
	public JSONObject save(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		String method = (String)map.remove("method");
		if("add".equals(method))
		{
			insert(map, ro);
		}
		else
		{
			update(map, ro);
		}
		return (JSONObject)JSON.toJSON(ro);
	}
	
	private void insert(Map map, ResultObject ro)
	{
		try
		{
			String menuId = (String)map.get("MENUID");
			if(!this.menuDao.isExistMenuId(menuId))
			{
				this.menuDao.insert(map);
				String actions = (String)map.get("actions");
				JSONArray actionAry = JSON.parseArray(actions);
				saveMenuAction(menuId, actionAry);
			}
			else
			{
				ro.changeFaultState("菜单编码已存在");
			}
		}
		catch(Exception e)
		{
			ro.changeFaultState("新增菜单失败");
			throw new RuntimeException("新增菜单失败", e);
		}
	}
	
	public void update(Map map, ResultObject ro) {
		try
		{
			this.menuDao.updateForPrimaryKey(map);
			String menuId = (String)map.get("MENUID");
			this.menuActionDAO.deleteByMenuId(menuId);
			String actions = (String)map.get("actions");
			JSONArray actionAry = JSON.parseArray(actions);
			saveMenuAction(menuId, actionAry);
			String deleteRoleMenuAction = (String)map.get("deleteRoleMenuAction");
			if("true".equals(deleteRoleMenuAction))
			{
				this.roleMenuActionDAO.deleteByMenuId(menuId);
			}
		}
		catch(Exception e)
		{
			ro.changeFaultState("修改菜单信息失败");
			throw new RuntimeException("修改菜单信息失败", e);
		}
	}

	private void saveMenuAction(String menuId, JSONArray actionAry)
	{
		if(actionAry.size() > 0)
		{
			this.menuActionDAO.insert(menuId, actionAry);
		}
	}

	@Override
	@CacheEvict(value={"queryMenu"}, allEntries=true)
	public JSONObject delete(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		this.menuDao.deleteForPrimaryKey(map);
		String menuId = map.get("MENUID");
		this.menuActionDAO.deleteByMenuId(menuId);
		this.roleMenuDAO.deleteByMenuId(menuId);
		this.roleMenuActionDAO.deleteByMenuId(menuId);
		return (JSONObject)JSON.toJSON(ro);
	}

	@Override
	public JSONArray queryFormList(String name, String isLink){
		name = StringUtils.isEmpty(name) ? "" : name;
		String result = formFeignService.queryFormList(TABLE_NAME, name, isLink);
		JSONArray ary = JSON.parseArray(result);
		return ary;
	}
}
